package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.Collection;
import com.example.yuejian.yuejian.bean.IsFollow;
import com.example.yuejian.yuejian.bean.MasterInfo;
import com.example.yuejian.yuejian.fragment.ClassFragment;
import com.example.yuejian.yuejian.fragment.QuestionFragment;
import com.example.yuejian.yuejian.model.CollectionModel;
import com.example.yuejian.yuejian.model.MastersModel;
import com.squareup.picasso.Picasso;


public class TeacherDetailActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout mTabClass;
    private LinearLayout mTabQuestion;

    private LinearLayout mTabAll;

    private TextView tvClass;
    private TextView tvQuestion;

    private Fragment mFmClass;
    private Fragment mFmQuestion;

    private  TextView master_intro;
    private ImageView master_photo;
    private  ImageView btn_master_follow;
    int master_id;
    private ImageView btn_back;

    private ModleListener<MasterInfo> modleListener=new ModleListener<MasterInfo>() {
        @Override
        public void onResponse(MasterInfo o) {
            MasterInfo res=o;
              master_intro.setText(res.getIntro());
            if (!"".equals(res.getPhoto()))
            {
                Picasso.with(getApplication()).load(res.getPhoto()).into(master_photo);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private ModleListener<Collection>followlistener=new ModleListener<Collection>() {
        @Override
        public void onResponse(Collection o) {
            Collection res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_master_follow.setImageResource(R.drawable.ic_myfollow);
                Toast.makeText(TeacherDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
            else
            {
                btn_master_follow.setImageResource(R.drawable.ic_follow);
                Toast.makeText(TeacherDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(TeacherDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };
    private ModleListener<IsFollow> isfollowlistener=new ModleListener<IsFollow>() {
        @Override
        public void onResponse(IsFollow o) {
            IsFollow res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_master_follow.setImageResource(R.drawable.ic_follow);
            }
            else
            {
                btn_master_follow.setImageResource(R.drawable.ic_myfollow);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(TeacherDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_detail);
        initViews();
        initEvents();
        setSelect(0);
        getData();
        isFollow();
        back();
    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_master_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private void getData()
    {

        Intent intent=getIntent();
         master_id= Integer.parseInt(intent.getStringExtra("master_id")) ;
        MastersModel mastersModel=new MastersModel();
        mastersModel.masterInfo(master_id,modleListener);
    }

    private void isFollow()
    {
        int collection_id=master_id;
        //int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
       collectionModel.isfollow(getUser_id(),3,collection_id,isfollowlistener);

    }
    private  void doFollow()
    {
        int collection_id=master_id;
        int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
        collectionModel.followMaster(user_id,3,collection_id,followlistener);

    }

    private void initViews()
    {
        mTabClass=(LinearLayout)findViewById(R.id.id_tab_teacher_detail_class);
        mTabQuestion=(LinearLayout)findViewById(R.id.id_tab_teacher_detail_question);


        mTabAll=(LinearLayout)findViewById(R.id.id_tab_teacher_detail_top_all);

        tvClass=(TextView)findViewById(R.id.tv_tab_teacher_detail_class);
        tvQuestion=(TextView)findViewById(R.id.tv_tab_teacher_detail_question);

        master_intro=(TextView)findViewById(R.id.teacher_detail_intro);
        btn_master_follow=(ImageView)findViewById(R.id.img_master_btn_follow);
        master_photo=(ImageView)findViewById(R.id.img_teacher_detail);

    }
    private void initEvents(){
        mTabClass.setOnClickListener(this);
        mTabQuestion.setOnClickListener(this);
        btn_master_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doFollow();
            }
        });

    }

    private void resetTextColors() {

        tvClass.setTextColor(tvClass.getResources().getColor(R.color.gray));
        tvQuestion.setTextColor(tvQuestion.getResources().getColor(R.color.gray));

    }
    private  void resetBgColors(){
        mTabClass.setBackgroundResource(R.drawable.shape_home_top);
        mTabQuestion.setBackgroundResource(R.drawable.shape_home_top);

        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_teacher_detail_class:
                setSelect(0);
                break;
            case R.id.id_tab_teacher_detail_question:
                setSelect(1);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i) {
      FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmClass== null)
                {
                    mFmClass = new ClassFragment();
                    FragmentTransaction add = transaction.add(R.id.id_content_teacher_detail, mFmClass);
                } else
                {
                    transaction.show(mFmClass);
                }
                tvClass.setTextColor(tvClass.getResources().getColor( R.color.white));
                mTabClass.setBackgroundColor(mTabClass.getResources().getColor( R.color.gray));

                break;
            case 1:
                if (mFmQuestion == null)
                {
                    mFmQuestion = new QuestionFragment();
                    transaction.add(R.id.id_content_teacher_detail, mFmQuestion);
                } else
                {
                    transaction.show(mFmQuestion);

                }

                tvQuestion.setTextColor(tvQuestion.getResources().getColor( R.color.white));
                mTabQuestion.setBackgroundColor(getResources().getColor(R.color.gray));

                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmClass != null)
        {
            transaction.hide(mFmClass);
        }
        if (mFmQuestion != null)
        {
            transaction.hide(mFmQuestion);
        }
    }
}
